Copyright © Wei MEI, MLMS™—all rights reserved. 🀤
Complex condition checks¶
Conditional execution can be completed using the if statement.
if syntax
>>> if expression:
# code to execute
elif expression:
# code to execute
else:
# code to execute
```
Boolean values can be either False or True
- x *or* y - If either x OR y is true, the expression is executed
- x *and* y - If x AND y are both true, the expression is executed
- < less than
- < greater than
- == is equal to
- >= greater than or equal to
- <= less than or equal to
- != not equal to
- x *in* [a,b,c] Does x match the value of a, b, or c
1 2 3 4 5 6 7 8 9 | # A student makes honour roll if their average is >=85
# and their lowest grade is not below 70
gpa = float(input('What was your Grade Point Average? '))
lowest_grade = input('What was your lowest grade? ')
lowest_grade = float(lowest_grade)
if gpa >= .85 and lowest_grade >= .70:
print('You made the honour roll')
|
Demo: dates¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # I check to see if the requirements for honour roll are met
gpa = float(input('What was your Grade Point Average? '))
lowest_grade = float(input('What was your lowest grade? '))
# Boolean variables allow you to remember a True/False value
if gpa >= .85 and lowest_grade >= .70:
honour_roll = True
else:
honour_roll = False
# Somewhere later in your code if you need to check if students is
# on honour roll, all I need to do is check the boolean variable
# I set earlier in my code
if honour_roll:
print('You made honour roll')
|
PPT Demonstrations¶
Challenges time¶
Check the following script and try to find the mistake:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # When you join a hockey team you get your name on the back of the jersey
# but the jersey may not be big enough to hold all the letters
# Ask the user for their first name
# Ask the user for their last name
# if first name is < 10 characters and last name is < 10 characters
# print first and last name on the jersey
# if first name >= 10 characters long and last name is < 10 characters
# print first initial of first name and the entire last name
# if first name < 10 characters long and last name is >= 10 characters
# print entire first name and first initial of last name
# if first name >= 10 characters long and last name is >= 10 characters
# print last name only
# Test with the following values
# first name: Susan last name: Ibach
# output: Susan Ibach
# first name: Susan last name: ReallyLongLastName
# output: Susan R.
# first name: ReallyLongFirstName last name: Ibach
# output: R. Ibach
# first name: ReallyLongFirstName last name: ReallyLongLastName
# output: ReallyLongLastName
|
solutions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | # When you join a hockey team you get your name on the back of the jersey
# but the jersey may not be big enough to hold all the letters
# Ask the user for their first name
first_name = input('Please enter your first name: ')
# Ask the user for their last name
last_name = input('Please enter your last name: ')
# if first name is < 10 characters and last name is < 10 characters
# print first and last name on the jersey
# if first name >= 10 characters long and last name is < 10 characters
# print first initial of first name and the entire last name
# if first name < 10 characters long and last name is >= 10 characters
# print entire first name and first initial of last name
# if first name >= 10 characters long and last name is >= 10 characters
# print last name only
# Check length of first name
if len(first_name) >=10:
long_first_name = True
else:
long_first_name = False
# Check length of last name
if len(last_name) >= 10:
long_last_name = True
else:
long_last_name = False
# Evaluate possible jersey print combinations for different lengths
if long_first_name and long_last_name:
print(last_name)
elif long_first_name:
print(first_name[0:1] + '. ' + last_name)
elif long_last_name:
print(first_name + ' ' + last_name[0:1] + '.')
else:
print(first_name + ' ' + last_name)
|